package lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: feiwang_6
 * @create: 2020/5/3 9:12
 * @description:
 */
public class MyServer {
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private volatile boolean isPro = true;
    
    public void get(){
        try {
            lock.lock();
            while (!isPro){
                condition.await();
            }
            isPro = false;
            System.out.println("消费消息****");
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    
    public void set(){
        try {
            lock.lock();
            while(isPro){
                condition.await();
            }
            isPro = true;
            System.out.println("生产消息####");
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
